

local CommonButton = class("CommonButton", function ()

	local  layer = display.newLayer()
	return layer 
end)

function CommonButton:setColor(c)
	
	local child = self:getChildByTag(1)
	child:setColor(c)
end

function CommonButton:onClick()
	if self.listener then
		self.listener()
	end
end

function CommonButton:onButtonDown()
	self:setColor(ccc3(200, 200, 200))
end

function CommonButton:onButtonUp()
	self:setColor(ccc3(255, 255, 255))

	self:onClick()
end

function CommonButton:ctor(data)
	local img = data.img
	local sprite = display.newSprite(img)
	sprite:setPosition(sprite:getContentSize().width / 2, sprite:getContentSize().height / 2)
	self:addChild(sprite, 1, 1)

	self.listener = data.listener


 	self:setTouchEnabled(true)

 	local boundBox = sprite:boundingBox()

 	local function onTouchEvent(event, x, y)
		if event == 'began' then
			if boundBox:containsPoint(sprite:convertToNodeSpace(CCPointMake(x, y))) then
				self:onButtonDown()
				return true
			else
				return false
			end
			
		elseif event == 'ended' then
			self:onButtonUp()
		end
 	end
    self:registerScriptTouchHandler(onTouchEvent)
 end

 return CommonButton
